package com.jyw.web.controller.system;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jyw.common.annotation.Anonymous;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.domain.dto.TeamAddDto;
import com.jyw.system.domain.dto.TeamEditDto;
import com.jyw.system.domain.vo.DeptCacheVo;
import com.jyw.system.domain.vo.SysTeamUserOneVO;
import com.jyw.system.domain.vo.SysTeamUserVO;
import com.jyw.system.domain.vo.TeamCacheVo;
import com.jyw.system.service.ISysUserTeamService;
import com.jyw.system.utils.TeamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.AjaxResult;
import com.jyw.common.enums.BusinessType;
import com.jyw.system.domain.SysTeam;
import com.jyw.system.service.ISysTeamService;
import com.jyw.common.utils.poi.ExcelUtil;
import com.jyw.common.core.page.TableDataInfo;

/**
 * 小组管理Controller
 *
 * @author system
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/system/team")
@Api(tags = "小组管理")
public class SysTeamController extends BaseController {
    @Autowired
    private ISysTeamService sysTeamService;

    @Resource
    private ISysUserTeamService userTeamService;

    /**
     * 查询小组管理列表
     */
    @GetMapping("/list")
    @ApiOperation("小组列表")
    public TableDataInfo<SysTeam> list(SysTeam sysTeam) {
        startPage();
        List<SysTeam> list = sysTeamService.selectSysTeamList(sysTeam);
        return getDataTable(list);
    }

    /**
     * 导出小组管理列表
     */
    @Log(title = "小组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "小组导出", hidden = true)
    public void export(HttpServletResponse response, SysTeam sysTeam) {
        List<SysTeam> list = sysTeamService.selectSysTeamList(sysTeam);
        ExcelUtil<SysTeam> util = new ExcelUtil<SysTeam>(SysTeam.class);
        util.exportExcel(response, list, "小组管理数据");
    }

    /**
     * 获取小组管理详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("小组详情")
    public CommonResult<SysTeam> getInfo(@PathVariable("id") Long id) {
        return CommonResult.success(sysTeamService.selectSysTeamById(id));
    }

    /**
     * 新增小组管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:add')")
    @Log(title = "小组管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增小组")
    public CommonResult add(@RequestBody TeamAddDto teamAddDto) {
        SysTeam sysTeam = teamAddDto.buildSysTeam();
        return CommonResult.success(sysTeamService.insertSysTeam(sysTeam));
    }

    /**
     * 修改小组管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:edit')")
    @Log(title = "小组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改小组")
    public CommonResult edit(@RequestBody TeamEditDto teamEditDto) {
        SysTeam sysTeam = teamEditDto.buildSysTeam();
        return CommonResult.success(sysTeamService.updateSysTeam(sysTeam));
    }

    /**
     * 删除小组管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:remove')")
    @Log(title = "小组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除小组")
    public CommonResult remove(@PathVariable Long[] ids) {
        return CommonResult.success(sysTeamService.deleteSysTeamByIds(ids));
    }

    @ApiOperation("获取小组缓存")
    @GetMapping("cache")
    @Anonymous
    public CommonResult<List<TeamCacheVo>> cache() {
        return CommonResult.success(sysTeamService.getCache());
    }


    @GetMapping("/getSysTeamUser")
    @ApiOperation("小组人员")
    @Anonymous
    public CommonResult<List<SysTeamUserOneVO>> getSysTeamUser() {
        return CommonResult.success(sysTeamService.getSysTeamUser());
    }

    @GetMapping("/mine")
    @ApiOperation("我的小组")
    @Anonymous
    public CommonResult<List<SysTeam>> getMineTeam() {
        List<Long> teamIds = userTeamService.getTeamIdByUser(SecurityUtils.getUserId());
        List<SysTeam> teams = TeamUtils.cacheList().stream().filter(data -> teamIds.contains(data.getId())).collect(Collectors.toList());
        return CommonResult.success(teams);
    }

}
